package com.superhero.miniprogram.controller;

import cn.hutool.core.util.IdcardUtil;
import com.superhero.common.exception.BusinessException;
import com.superhero.common.pojo.ApiResult;
import com.superhero.common.pojo.PageInput;
import com.superhero.common.pojo.vo.HealthInfoInput;
import com.superhero.common.pojo.vo.HealthInfoVo;
import com.superhero.common.pojo.vo.PageVo;
import com.superhero.common.pojo.vo.UserEnd.UserInfoVo;
import com.superhero.miniprogram.security.UserUtil;
import com.superhero.miniprogram.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@Api(tags = {"用户端"})
@RestController
@RequestMapping("/miniprogram/user")
public class UserController {

    @Autowired
    UserServiceImpl userService;

    @ApiOperation(value = "获取用户基本信息")
    @GetMapping("/healthQRCode")
    @PreAuthorize("hasAnyRole('UNCERTIFICATION','HOUSEHOLD','FAIL')")
    public ApiResult<UserInfoVo> getHealthQRCode() {
        String username = UserUtil.getUserUsername();
        UserInfoVo vo = userService.getHealthQRCode(username);

        return ApiResult.success(vo);
    }

    @ApiOperation(value = "健康登记")
    @PostMapping("/healthInfo")
    @PreAuthorize("hasAnyRole('UNCERTIFICATION','HOUSEHOLD','FAIL')")
    public ApiResult createHealthInfo(@Valid HealthInfoInput healthInfoInput) throws Exception {
        if (!IdcardUtil.isValidCard(healthInfoInput.getIdCard())) {
            throw new BusinessException("身份证输入不合法");
        }
        String username = UserUtil.getUserUsername();
        userService.createHealthInfo(username, healthInfoInput);

        return ApiResult.success();
    }

    @ApiOperation(value = "修改健康登记表")
    @PutMapping("/healthInfo")
    @PreAuthorize("hasAnyRole('UNCERTIFICATION','HOUSEHOLD','FAIL')")
    public ApiResult UpdateHealthInfo(@Valid HealthInfoInput healthInfoInput) {
        if (!IdcardUtil.isValidCard(healthInfoInput.getIdCard())) {
            throw new BusinessException("身份证输入不合法");
        }
        String username = UserUtil.getUserUsername();
        userService.UpdateHealthInfo(username, healthInfoInput);

        return ApiResult.success();
    }

    @ApiOperation(value = "获取健康登记表记录")
    @GetMapping("/healthInfo")
    @PreAuthorize("hasAnyRole('UNCERTIFICATION','HOUSEHOLD','FAIL')")
    public ApiResult<HealthInfoVo> getHealthInfo() {
        String username = UserUtil.getUserUsername();
        HealthInfoVo vo = userService.getHealthInfo(username);

        return ApiResult.success(vo);
    }

    @ApiOperation(value = "获取出入社区登记表记录", notes = "key可为检查人员姓名、出入类型（出/入），检查人员手机号，社区名称的全模糊查询")
    @GetMapping("/accessInfo")
    @PreAuthorize("hasAnyRole('HOUSEHOLD')")
    public ApiResult<PageVo> getAccessInfoList(@Valid PageInput pageInput, String keyword) {
        return ApiResult.success(userService.getAccessInfoList(pageInput, keyword));
    }

}
